﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EmailMarketingCampaign
{
    public partial class mailSelect : System.Web.UI.Page
    {

        CampaignControl cc = new CampaignControl();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                if (((Campaign)Session["campaign"]).getTo().ToString() != "")
                {
                    string to = ((Campaign)Session["campaign"]).getTo().ToString();

                    string[] b = to.Split(' ', ',');

                    if (b[0] == "0" && b[1] != "0" && b[2] != "0")
                    {
                        drGroup2.SelectedValue = b[1];
                        drGroup3.SelectedValue = b[2];

                    }
                    if (b[0] == "0" && b[1] != "0" && b[2] == "0")
                    {
                        drGroup1.SelectedValue = b[0];
                        drGroup3.SelectedValue = b[2];

                    }
                    if (b[0] == "0" && b[1] == "0" && b[2] != "0")
                    {
                        drGroup1.SelectedValue = b[0];
                        drGroup2.SelectedValue = b[1];

                    }

                    if (b[0] != "0" && b[1] == "0" && b[2] == "0")
                    {
                        drGroup1.SelectedValue = b[0];


                    }
                    if (b[0] != "0" && b[1] != "0" && b[2] == "0")
                    {
                        drGroup1.SelectedValue = b[0];
                        drGroup2.SelectedValue = b[1];

                    }
                    if (b[0] != "0" && b[1] == "0" && b[2] != "0")
                    {
                        drGroup1.SelectedValue = b[0];
                        drGroup3.SelectedValue = b[2];

                    }



                    if (b[1] == "0" && b[0] != "0" && b[2] != "0")
                    {
                        drGroup2.SelectedValue = b[1];
                        drGroup3.SelectedValue = b[2];

                    }
                    if (b[1] == "0" && b[0] == "0" && b[2] != "0")
                    {
                        drGroup2.SelectedValue = b[1];


                    }

                    if (b[1] == "0" && b[0] != "0" && b[2] == "0")
                    {
                        drGroup1.SelectedValue = b[0];


                    }


                    if (b[1] != "0" && b[0] == "0" && b[2] == "0")
                    {
                        drGroup2.SelectedValue = b[1];

                    }
                    if (b[1] != "0" && b[0] != "0" && b[2] == "0")
                    {
                        drGroup2.SelectedValue = b[1];
                        drGroup1.SelectedValue = b[0];

                    }
                    if (b[1] != "0" && b[0] == "0" && b[2] != "0")
                    {
                        drGroup2.SelectedValue = b[1];
                        drGroup3.SelectedValue = b[2];

                    }




                    if (b[2] == "0" && b[1] != "0" && b[0] != "0")
                    {
                        drGroup1.SelectedValue = b[0];
                        drGroup2.SelectedValue = b[1];

                    }
                    if (b[2] == "0" && b[1] != "0" && b[0] == "0")
                    {

                        drGroup2.SelectedValue = b[1];

                    }
                    if (b[2] == "0" && b[1] == "0" && b[0] != "0")
                    {
                        drGroup1.SelectedValue = b[0];


                    }


                    if (b[2] != "0" && b[1] == "0" && b[0] == "0")
                    {
                        drGroup3.SelectedValue = b[2];

                    }
                    if (b[2] != "0" && b[1] != "0" && b[0] == "0")
                    {
                        drGroup3.SelectedValue = b[2];
                        drGroup2.SelectedValue = b[1];

                    }
                    if (b[2] != "0" && b[1] == "0" && b[0] != "0")
                    {
                        drGroup3.SelectedValue = b[2];
                        drGroup1.SelectedValue = b[0];

                    }


                    if (b[0] != "0" && b[1] != "0" && b[2] != "0")
                    {
                        drGroup1.SelectedValue = b[0];
                        drGroup2.SelectedValue = b[1];
                        drGroup3.SelectedValue = b[2];

                    }



                }

            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string group1 = "", group2 = "", group3 = "", group = "";

            if (drGroup1.SelectedItem.Text != "==select==")
            {
                group1 = drGroup1.SelectedValue.ToString() + ",";

            }
            else { group1 = "0" + ","; }
            if (drGroup2.SelectedItem.Text != "==select==")
            {
                group2 = drGroup2.SelectedValue.ToString() + ",";

            }
            else { group2 = "0" + ","; }
            if (drGroup3.SelectedItem.Text != "==select==")
            {
                group3 = drGroup3.SelectedValue.ToString();

            }
            else { group3 = "0"; }


            group = group1 + group2 + group3;


            cc.updateCampaignTo(group, ((Campaign)Session["campaign"]));
            ((Campaign)Session["campaign"]).setTo(group);


        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            string group1 = "", group2 = "", group3 = "", group = "";

            if (drGroup1.SelectedItem.Text != "==select==")
            {
                group1 = drGroup1.SelectedValue.ToString() + ",";

            }
            else { group1 = "0" + ","; }
            if (drGroup2.SelectedItem.Text != "==select==")
            {
                group2 = drGroup2.SelectedValue.ToString() + ",";

            }
            else { group2 = "0" + ","; }
            if (drGroup3.SelectedItem.Text != "==select==")
            {
                group3 = drGroup3.SelectedValue.ToString();

            }
            else { group3 = "0"; }


            group = group1 + group2 + group3;


            cc.updateCampaignTo(group, ((Campaign)Session["campaign"]));
            ((Campaign)Session["campaign"]).setTo(group);

            Response.Redirect("SendEmail.aspx");
        }



        protected void drGroup1_PreRender(object sender, EventArgs e)
        {
            ListItem item = new ListItem("==select==", "0");

            if (drGroup1.Items.Contains(item)) { }

            else this.drGroup1.Items.Insert(0, item);

        }

        protected void drGroup3_PreRender(object sender, EventArgs e)
        {
            ListItem item = new ListItem("==select==", "0");

            if (drGroup3.Items.Contains(item)) { }

            else this.drGroup3.Items.Insert(0, item);
        }

        protected void drGroup2_PreRender(object sender, EventArgs e)
        {
            ListItem item = new ListItem("==select==", "0");

            if (drGroup2.Items.Contains(item)) { }

            else this.drGroup2.Items.Insert(0, item);


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditMessage.aspx");
        }
    }
}
